/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Courses;
import hibernate.entity.Staffs;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class StaffDAO extends AbstractDAO {

    /**
     * @return List<Staffs>
     * @see Whole list of staffs
     */
    public List<Staffs> readAll() {
        query = session.createQuery("from Staffs");
        return query.list();
    }

    /**
     * @param id an id represents for staffid to get a correlative staff
     * @return Staffs
     * @see Staffs
     */
    public Staffs readByID(int id) {
        try {
            query = session.createQuery("from Staffs where staffId = :id and status = 1");
            query.setParameter("id", id);
            return (Staffs) query.list().get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param Staffs a staff choosen to remove
     * @return int
     */
    public int remove(Staffs st) {
        try {
            tx = session.beginTransaction();
            st.setStatus(-1);
            st = (Staffs) session.merge(st);
            session.update(st);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @return List<Staffs> a list of staffs that is in the University right now
     */
    public List<Staffs> readLiving() {
        query = session.createQuery("from Staffs where status = 1");
        return query.list();
    }

    /**
     * @param i represent for status of staffs
     * @return List<Staffs>
     */
    public List<Staffs> readByStatus(int i) {
        try {
            query = session.createQuery("from Staffs where status = :i");
            query.setParameter("i", i);
            return query.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param Staffs a staff's edit info to update
     * @return int
     * @see a list of staffs
     */
    public int update(Staffs st) {
        try {
            tx = session.beginTransaction();
            st = (Staffs) session.merge(st);
            session.update(st);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * @param keyword a word, entered by user, to search a correlative list of
     * staffs
     * @return List<Staffs>
     * @see a list of staffs
     */
    public List<Staffs> search(String keyword) {
        query = session.createQuery("from Staffs st where st.accounts.fullName like '%" + keyword + "%' "
                + "or st.accounts.userName like '%" + keyword + "%' "
                + "or st.accounts.fullName like '%" + keyword + "%' and st.status = 1");
        return query.list();
    }

    /**
     * @param Courses a course for finding all of staffs that have not had their
     * courses to manage
     * @return List<Staffs>
     * @see a list of staffs
     */
    public List<Staffs> outOfCourse(Courses c) {
        try {
            query = session.createQuery("select s from Staffs s WHERE :c not in elements (s.courseses) and s.status >0 ");
            query.setParameter("c", c);
            return query.list();
        } catch (Exception e) {
            return null;
        }
    }
}
